package com.imddysc.spbbatchitemwriterdb;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.sql.DataSource;

import org.springframework.batch.core.Job;
import org.springframework.batch.core.Step;
import org.springframework.batch.core.StepContribution;
import org.springframework.batch.core.configuration.annotation.EnableBatchProcessing;
import org.springframework.batch.core.configuration.annotation.JobBuilderFactory;
import org.springframework.batch.core.configuration.annotation.StepBuilderFactory;
import org.springframework.batch.core.configuration.annotation.StepScope;
import org.springframework.batch.core.job.flow.JobExecutionDecider;
import org.springframework.batch.core.launch.support.RunIdIncrementer;
import org.springframework.batch.core.scope.context.ChunkContext;
import org.springframework.batch.core.step.tasklet.Tasklet;
import org.springframework.batch.item.ItemProcessor;
import org.springframework.batch.item.ItemReader;
import org.springframework.batch.item.ItemWriter;
import org.springframework.batch.item.database.BeanPropertyItemSqlParameterSourceProvider;
import org.springframework.batch.item.database.JdbcBatchItemWriter;
import org.springframework.batch.item.database.JdbcPagingItemReader;
import org.springframework.batch.item.database.Order;
import org.springframework.batch.item.database.support.MySqlPagingQueryProvider;
import org.springframework.batch.repeat.RepeatStatus;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jdbc.core.RowMapper;

import com.imddysc.spbbatchitemprocessor.Customer;

@Configuration
@EnableBatchProcessing
public class DbItemWriterDemo {
	
	@Autowired
	private JobBuilderFactory jobBuilderFactory;
	
	@Autowired
	private StepBuilderFactory stepBuilderFactory;

	@Autowired
	private ItemProcessor<Customer, Customer> firstNameUpperProcessor;

	@Autowired
	private DataSource dataSource;
	
	@Bean
	public Job dbItemWriterDemoJob() {
		return jobBuilderFactory.get("dbItemWriterDemoJob2")
				.incrementer(new RunIdIncrementer())
				.start(dbItemWriterDemoStep())
				.build();
	}

	@Bean
	public Step dbItemWriterDemoStep() {
		return stepBuilderFactory.get("dbItemWriterDemoStep2")
				.<Customer,Customer>chunk(2)
				.reader(getItemReader())
				.processor(firstNameUpperProcessor)
				.writer(getItemWriter())
				.build();
	}


	@Bean
	@StepScope
	public JdbcPagingItemReader<Customer> getItemReader() {
		JdbcPagingItemReader<Customer> reader = new JdbcPagingItemReader<Customer>();
		reader.setDataSource(dataSource);
		// 设置读取缓存
		reader.setFetchSize(2);
		// 把读取到的记录转换成User对象
		reader.setRowMapper(new RowMapper<Customer>() {

			@Override
			public Customer mapRow(ResultSet resultSet, int rowNum) throws SQLException {
				Customer customer = new Customer();
				customer.setId(resultSet.getLong("id"));
				customer.setFirstName(resultSet.getString("firstName"));
				customer.setLastName(resultSet.getString("lastName"));
				customer.setBirthday(resultSet.getString("birthday"));
				return customer;
			}
		});
		// 指定sq1语句
		MySqlPagingQueryProvider provider = new MySqlPagingQueryProvider();
		provider.setSelectClause("id,firstName,lastName,birthday");
		provider.setFromClause("from customer");
		// 指定根据哪个字段进行排序
		Map<String, Order> sort = new HashMap<>(1);
		sort.put("id", Order.ASCENDING);
		provider.setSortKeys(sort);
		reader.setQueryProvider(provider);
		return reader;
	}


	@Bean
	@StepScope
	public JdbcBatchItemWriter<Customer> getItemWriter() {
		JdbcBatchItemWriter<Customer> writer = new JdbcBatchItemWriter<Customer>();
		writer.setDataSource(dataSource);
		
		writer.setSql("insert into customer1(id,firstName,lastName,birthday) values " +
                "(:id,:firstName,:lastName,:birthday)");
		writer.setItemSqlParameterSourceProvider(new BeanPropertyItemSqlParameterSourceProvider<>());
		
		return writer;
	}

	@Bean
	@StepScope
	public ItemWriter<Customer> getItemWriter0() {
		return new ItemWriter<Customer>() {

			@Override
			public void write(List<? extends Customer> items) throws Exception {
				for (Customer cus : items) {
					System.out.println(cus);
				}
			}
		};
	}
	
	@Bean
	@StepScope
	public JdbcBatchItemWriter<Customer> getItemWriter2() {
		JdbcBatchItemWriter<Customer> writer = new JdbcBatchItemWriter<Customer>();
		writer.setDataSource(dataSource);
		
		writer.setSql("insert into customer(id,firstName,lastName,birthday) values " +
                "(:id,:firstName,:lastName,:birthday)");
		writer.setItemSqlParameterSourceProvider(new BeanPropertyItemSqlParameterSourceProvider<>());
		
		return writer;
	}

}
